//package com.elh.serurity.filter;
//
//import cn.hutool.core.date.DateUnit;
//import cn.hutool.core.date.DateUtil;
//import com.dataSource.security.entity.User;
//import com.dataSource.security.redis.RedisCache;
//import com.dataSource.utils.jwt.JwtUtil;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.security.core.Authentication;
//import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.stereotype.Component;
//import org.springframework.web.filter.OncePerRequestFilter;
//
//import javax.servlet.FilterChain;
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//import java.util.Date;
//import java.util.Objects;
//import java.util.concurrent.TimeUnit;
//
///**
// * @author liulei
// */
//@Component
//public class JwtAuthenticationFilter extends OncePerRequestFilter {
//
//    @Autowired
//    private JwtUtil jwtUtil;
//
//    @Autowired
//    private RedisCache redisCache;
//
//    @Override
//    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
//        // 获取token
//        String jwt = jwtUtil.getJwtFromRequest(request);
//        if(Objects.nonNull(jwt)){
//            Authentication authentication = jwtUtil.parseJWT(jwt);
//            Object principal = authentication.getPrincipal();
//            // 验证token
//
//            User user = (User) principal;
//            Date loginTime = new Date(user.getLoginTime());
//            long intervalDate = DateUtil.between(loginTime, new Date(), DateUnit.MINUTE);
//            // 已经登陆过的用户
//            Object cacheObject = redisCache.getCacheObject(jwt);
//            // 如果请求时间没有过20分钟的话那么再次刷新token
//            if(intervalDate < 20 && Objects.nonNull(cacheObject)){
//                // 刷新
//                redisCache.setCacheObject(jwt,user,20, TimeUnit.MINUTES);
//            }
//            // 将请求信息设置holder
//            SecurityContextHolder.getContext().setAuthentication(authentication);
//        }
//        filterChain.doFilter(request,response);
//    }
//
//
//
//}
